(function ($) {
    'use strict';
    $(function () {

        // 显示 modal 选项
        $(document).on('click', '.more-select', function (e) {
            var $modal = '#' + $(this).attr('id') + 'Modal';
            $(this).blur();
            Util.showModal($modal);
        });

        // 选中 modal 选项
        $(document).on('click', '.pmodal .select-list a', function (e) {
            var $this = $(this);
            var $modal = $this.parents('.pmodal');
            var $trigger = $('#' + $modal.attr('id').replace(/Modal$/, ''));
            var $input = $('[name="' + $modal.data('bind-name') + '"]');
            if ($modal.attr('id') == "provinceModal") {
                $("#province").addClass('selected').val("");
            }
            //选中样式
            $modal.find('.select-list a').removeClass('selected');
            $this.addClass('selected');
            //选中文本
            var str = $("#province").addClass('selected').val();
            if (str.indexOf($this.text()) < 0) {

                $("#province").addClass('selected').val(str + $this.text());
            }
            //$trigger.addClass('selected').text($this.text());
            $input.val($this.data('value'));
            Util.hideModal('.pmodal');


            if ($modal.attr('id') == "provinceModal") {
                $.ajax({
                    url: '/Address/Select',
                    type: 'post',
                    data: { parentno: $this.data('value') },
                    success: function (data) {
                        data = JSON.parse(data);
                        // 弹出层选择器
                        var theModal = $('#cityModal');
                        // 输入框选择器
                        var theInput = $('input[name="cityno"]');
                        var res = data;
                        // 更新面试城市
                        var rcValue = theInput.val();
                        var rcHtml = '';
                        var rcSelected = false;
                        for (var i = 0 ; i <= res.data.length - 1; i++) {
                            rcSelected = rcSelected || (rcValue == res.data[i].zipno ? true : false);
                            rcHtml += '<li><a class=\'' + (rcValue == res.data[i].zipno ? 'selected' : '') + '\' href="javascript:;" data-value="' + res.data[i].zipno + '">' + res.data[i].zipname + '</a></li>';
                        }
                        theModal.find('.select-list').html(rcHtml);
                        Util.showModal("#cityModal");

                        // 如果更新后的城市列表没有选中则清空城市表单并还原选择器
                        if (!rcSelected) {
                            theInput.val('');
                            $('input[name="countyno"]').val('');
                        };
                    }
                });
            }


            if ($modal.attr('id') == "cityModal") {
                $.ajax({
                    url: '/Address/Select',
                    type: 'post',
                    data: { parentno: $this.data('value') },
                    success: function (data) {
                                data = JSON.parse(data);
                        // 弹出层选择器
                        var theModal = $('#countyModal');
                        // 输入框选择器
                        var theInput = $('input[name="countyno"]');
                        var res = data;
                        // 更新面试城市
                        var rcValue = theInput.val();
                        var rcHtml = '';
                        var rcSelected = false;
                        for (var i = 0 ; i <= res.data.length - 1; i++) {
                            rcSelected = rcSelected || (rcValue == res.data[i].zipno ? true : false);
                            rcHtml += '<li><a class=\'' + (rcValue == res.data[i].zipno ? 'selected' : '') + '\' href="javascript:;" data-value="' + res.data[i].zipno + '">' + res.data[i].zipname + '</a></li>';
                        }
                        theModal.find('.select-list').html(rcHtml);
                        Util.showModal("#countyModal");

                        // 如果更新后的城市列表没有选中则清空城市表单并还原选择器
                        if (!rcSelected) {
                            theInput.val('');
                        };
                    }
                });
            }

            //if ($modal.attr('id') == "workCity1Modal") {
            //    Util.showModal("#workCity1Modal");
            //}
            // 检查选择器是否有回调事件
            var callback = $trigger.data('callback');
            if (callback) {
                $(document.body).trigger(callback);
            };
        });

        // 点击 modal 返回按钮后隐藏所有 modal
        $(document).on('click', '.pmodal-back', function (e) {
            var $modal = $(this).parents('.pmodal');
            var $trigger = $('#' + $modal.attr('id').replace(/Modal$/, ''));
            var $textarea = $modal.find('textarea');

            // 如果 modal 中包含 textarea 则将触发器的值设置为 textarea 的内容
            if ($textarea.length > 0) {
                var $content = $textarea.val().trim();
                if ($content.length > 0) {
                    $trigger.addClass('selected').text($content);
                } else {
                    $trigger.removeClass('selected').text($textarea.attr('placeholder'));
                }
            }

            Util.hideModal('.pmodal');
        });

    });
})(Zepto);
